﻿'// ========================================================================= '
'// ------------------------------------------------------------------------- '
'//!				\file		CLS_ImageDb.vb
'			        \date		june 2010
'			        \author		TNick
'
'			        \brief		Dialog for search results
'
'
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'   This file is considered universal knowledge. No restrictions
'   are imposed; use as you see fit.
'   Please read ReadMe.txt and Licence.txt in root folder
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'
'   
'
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
'//
'//
'//
'//
'//  IMPORTS     ------------------------------------------------------------ '
#Region "Imports"

Imports System.Windows.Forms
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices

#End Region
'//  IMPORTS     ============================================================ '
'//
'//
'//
'//
'//  DEFINITIONS    --------------------------------------------------------- '




'//  DEFINITIONS    ========================================================= '
'//
'//
'//
'//
'//  DATA    ---------------------------------------------------------------- '


'//  DATA    ================================================================ '
'//
'//
'//
'//
'//  CLASS    --------------------------------------------------------------- '

Public Class CLS_ImageDb

    Public Shared BkCol_Xreffed As System.Drawing.Color = Drawing.Color.Gold
    Public Shared FrCol_Inserted As System.Drawing.Color = Drawing.Color.Red
    Public FthClass As CM_ImageDb = Nothing
    ' The column currently used for sorting.
    Private m_SortingColumn As ColumnHeader




    '// --------------------------------------------------------------------- '
    Private Sub OK_Button_Click(ByVal sender As System.Object, _
                                ByVal e As System.EventArgs) _
                                Handles OK_Button.Click
        Me.Hide
        Dim acDoc As ApplicationServices.Document = _
            ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Using scDocL As DocumentLock = acDoc.LockDocument()
        Try
            If (acDoc Is Nothing) Then Exit Try
            If (Me.LW_Files.Enabled = True) Then
                Dim li As System.Windows.Forms.ListViewItem
                For each li in Me.LW_Files.Items
                    If (li.Checked) Then
                        Dim id As IMGDATA
                        id = DirectCast(li.Tag, IMGDATA)

                        CM_ImageDb.InsertRImage( _
                            acDoc, _
                            li.SubItems.Item(1).Text, _
                            li.Text, _
                            id.dNorth, id.dEast, id.dScale, id.dRot)
                    End If
                Next li
            End If
            
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
		Catch ex As System.Exception
            CMain.PopUpExc()
			CMain.DebugException(ex)
            Me.Show
		End Try
        End Using
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub Cancel_Button_Click(ByVal sender As System.Object, _
                                    ByVal e As System.EventArgs) _
                                    Handles Cancel_Button.Click
        
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub CLS_ImageDb_Load( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles MyBase.Load

        Me.Text = CMain._T(TX.IMGDBF) & ". " & _
                CMain._T(TX.SRCRES) & "."
        Me.OK_Button.Text = CMain._T(TX.OK)
        Me.Cancel_Button.Text = CMain._T(TX.CANCEL)

        Me.L_Xreffed.Text = CMain._T(TX.IMDB_INXREF)
        Me.L_Inserted.Text = CMain._T(TX.IMDB_INSRT)


        Me.LW_Files.CheckBoxes = True
        Me.LW_Files.Columns.Add(CMain._T(TX.FILE),180)
        Me.LW_Files.Columns.Add(CMain._T(TX.PATH),200)
        Me.LW_Files.Columns.Add(CMain._T(TX.NORTH),60)
        Me.LW_Files.Columns.Add(CMain._T(TX.EAST),60)
        Me.LW_Files.Columns.Add(CMain._T(TX.SCALE),60)
        Me.LW_Files.Columns.Add(CMain._T(TX.ROT),60)

        Me.LBCol_Back.BackColor = BkCol_Xreffed
        Me.LBCol_Front.BackColor = FrCol_Inserted
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub Append_img( _
        ByRef acDoc As ApplicationServices.Document, _
        ByRef acTrans As Transaction, _
        ByRef li As System.Windows.Forms.ListViewItem, _
        ByRef id As IMGDATA)

        Dim acDef As RasterImageDef = _
            CM_ImageDb.IsImgInDoc( _
                 acDoc, _
                 li.SubItems(1).Text, _
                 li.Text)
        If acDef IsNot Nothing Then
            li.BackColor = BkCol_Xreffed

            Dim acOIdCl As ObjectIdCollection = _
                CM_ImageDb.GetRefsImg(acDoc, acDef)
            If acOIdCl IsNot Nothing Then
                If (acOIdCl.Count > 0) Then
                    '// at least one instance is inserted
                    For Each acId As ObjectId In acOIdCl
                        Dim acImRf As RasterImage = _
                            DirectCast(acTrans.GetObject( _
                            acId, OpenMode.ForRead),  _
                            RasterImage)
                        Dim Out As IMGFD = Nothing
                        CM_ImageDb.GetRstInfo(acDoc, acDef, acImRf, Out)
                        '// now see if this is the same as
                        '// what we have in mind
                        If CM_ImageDb.IsSameLoc(id, Out.rtd) Then
                            '// this is exactly what we have in mind
                            li.ForeColor = FrCol_Inserted
                            Exit For
                        End If
                    Next
                Else
                    '// there is no instance
                    '// inserted in current drawing

                End If
            End If
        End If
        li.Tag = id

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Public Sub ShowMeFav(ByRef pFth As CM_ImageDb)
        Dim i As Integer


        If (CM_ImageDb.Stgs.sFavStr Is Nothing) Then Return

        Dim acDoc As ApplicationServices.Document = _
            ApplicationServices.Application.DocumentManager.MdiActiveDocument
        If (acDoc Is Nothing) Then Return
        Using scDocL As DocumentLock = acDoc.LockDocument()
            Using acTrans As Transaction = acDoc.Database. _
                TransactionManager.StartTransaction()
                Try

                    FthClass = pFth
                    Dim li As System.Windows.Forms.ListViewItem

                    If (FthClass Is Nothing) Then Exit Try
                    i = 0
                    For Each sE As FAVIMG In CM_ImageDb.Stgs.sFavStr
                            If (sE.idx > 0) Then
                                li = Me.LW_Files.Items.Add(sE.id.sName)
                                li.SubItems.Add(sE.id.sPath)
                                li.SubItems.Add(CStr(sE.id.dNorth))
                                li.SubItems.Add(CStr(sE.id.dEast))
                                li.SubItems.Add(CStr(sE.id.dScale))
                                li.SubItems.Add(CStr(sE.id.dRot))
                                li.Tag = i
                                Append_img(acDoc, acTrans, li, sE.id)
                            End If
                        i = i + 1
                    Next
                    Me.Text = CMain._T(TX.FAV)
                    ApplicationServices.Application.ShowModelessDialog(Me)
                Catch ex As System.Exception
                    CMain.PopUpExc()
                    CMain.DebugException(ex)
                End Try
            End Using
        End Using
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Public Sub ShowMeLastLst(ByRef pFth As CM_ImageDb)

        Dim acDoc As ApplicationServices.Document = _
            ApplicationServices.Application.DocumentManager.MdiActiveDocument
        If (acDoc Is Nothing) Then Return
        Using scDocL As DocumentLock = acDoc.LockDocument()
            Using acTrans As Transaction = acDoc.Database. _
                TransactionManager.StartTransaction()
                Try

                    FthClass = pFth
                    Dim li As System.Windows.Forms.ListViewItem

                    If (FthClass Is Nothing) Then Exit Try
                    If (FthClass.iAry Is Nothing) Then
                        CMain.AlertUser(TX.ERR_NOICHC)
                        Exit Try
                    End If

                    For i = 0 To UBound(FthClass.iAry)
                        If Not (FthClass.iAry(i).sName Is Nothing) Then
                            li = Me.LW_Files.Items.Add(FthClass.iAry(i).sName)
                            li.SubItems.Add(FthClass.iAry(i).sPath)
                            li.SubItems.Add(CStr(FthClass.iAry(i).dNorth))
                            li.SubItems.Add(CStr(FthClass.iAry(i).dEast))
                            li.SubItems.Add(CStr(FthClass.iAry(i).dScale))
                            li.SubItems.Add(CStr(FthClass.iAry(i).dRot))
                            li.Tag = i
                            Append_img(acDoc, acTrans, li, FthClass.iAry(i))
                        End If
                    Next
                    ApplicationServices.Application.ShowModelessDialog(Me)
                Catch ex As System.Exception
                    CMain.PopUpExc()
                    CMain.DebugException(ex)
                End Try
            End Using
        End Using
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub LW_Files_ColumnClick( _
        ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.ColumnClickEventArgs) _
        Handles LW_Files.ColumnClick

        '// code copied from
        '// http://www.vb-helper.com/howto_net_listview_sort_clicked_column.html

        ' Get the new sorting column.
        Dim new_sorting_column As ColumnHeader = _
            Me.LW_Files.Columns(e.Column)

        ' Figure out the new sorting order.
        Dim sort_order As System.Windows.Forms.SortOrder
        If m_SortingColumn Is Nothing Then
            ' New column. Sort ascending.
            sort_order = SortOrder.Ascending
        Else
            ' See if this is the same column.
            If new_sorting_column.Equals(m_SortingColumn) Then
                ' Same column. Switch the sort order.
                If m_SortingColumn.Text.StartsWith("^") Then
                    sort_order = SortOrder.Descending
                Else
                    sort_order = SortOrder.Ascending
                End If
            Else
                ' New column. Sort ascending.
                sort_order = SortOrder.Ascending
            End If

            ' Remove the old sort indicator.
            m_SortingColumn.Text = _
                m_SortingColumn.Text.Substring(2)
        End If

        ' Display the new sort order.
        m_SortingColumn = new_sorting_column
        If sort_order = SortOrder.Ascending Then
            m_SortingColumn.Text = "^ " & m_SortingColumn.Text
        Else
            m_SortingColumn.Text = "v " & m_SortingColumn.Text
        End If

        ' Create a comparer.
        Me.LW_Files.ListViewItemSorter = New  _
            ListViewComparer(e.Column, sort_order)

        ' Sort.
        Me.LW_Files.Sort()

    End Sub
    '// ===================================================================== '

End Class

'//  CLASS    =============================================================== '
'//
'//
'//
'//
'//  FUNCTIONS    ----------------------------------------------------------- '

'//  FUNCTIONS    =========================================================== '
'//
'//
'//
'//
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
